import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { StepTaskModel } from '#/api/process/models';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'taskCode',
      label: '工步编号',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'taskName',
      label: '工步名称',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        max: 31_000,
        step: 1000,
        precision: 0,
      },
      fieldName: 'seq',
      label: '顺序号',
      defaultValue: 0,
      help: '同一工艺步骤中顺序号必须唯一，分组区间基数为 1000，设为 0 或是 1000 的倍数时会在区间内进行自增',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 0,
      },
      fieldName: 'standardTime',
      label: '作业时间(s)',
      defaultValue: 0,
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '否', value: false },
          { label: '是', value: true },
        ],
      },
      fieldName: 'isCheckStandard',
      label: '校验标准',
      defaultValue: false,
      rules: 'required',
      help: '设置后将由系统来判定结果，会将采集数据与对应的工艺标准中设定的上下限进行对比，得出单项判定结果以及最终判定结果',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 255,
      },
      fieldName: 'workInstruction',
      label: '操作说明',
    },
  ];
}

/**
 * 获取复制表单的字段配置。
 */
export function useCopySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'taskCodePrefix',
      label: '工步编号前缀',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'taskNamePrefix',
      label: '工步名称前缀',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        max: 100,
        precision: 0,
      },
      fieldName: 'count',
      label: '复制数量',
      defaultValue: 10,
      rules: 'required',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<StepTaskModel>,
): VxeTableGridOptions<StepTaskModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'taskCode', title: '工步代码', width: 160 },
    { field: 'taskName', title: '工步名称', minWidth: 120 },
    { field: 'seq', title: '顺序号', width: 80 },
    { field: 'standardTime', title: '作业时长(s)', width: 100 },
    {
      field: 'isCheckStandard',
      title: '校验工艺标准',
      width: 110,
      formatter: ({ cellValue }) => (cellValue ? '是' : '否'),
    },
    {
      cellRender: {
        attrs: {
          nameField: 'taskName',
          nameTitle: '工步',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          { code: 'open', text: '标准' },
          'edit',
          { code: 'copy', text: '复制' },
          'delete',
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 180,
    },
  ];
}
